package com.qs.base.utils;

import android.app.AlertDialog;
import android.app.DatePickerDialog;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * time 工具类
 */

public class BaseTimeUtils {
    public static int THEME_DEVICE_DEFAULT_LIGHT = AlertDialog.THEME_DEVICE_DEFAULT_LIGHT;
    public static int THEME_DEVICE_DEFAULT_DARK = AlertDialog.THEME_DEVICE_DEFAULT_DARK;
    public static int THEME_TRADITIONAL = AlertDialog.THEME_TRADITIONAL;
    public static int THEME_HOLO_LIGHT = AlertDialog.THEME_HOLO_LIGHT;
    public static int THEME_HOLO_DARK = AlertDialog.THEME_HOLO_DARK;
    private static DatePickerDialog mDatePickerDialog;//日期选择器

    /**
     * 将字符串时间转为Long时间
     *
     * @param time yyyy-MM-dd HH:mm:ss:SSS
     */
    public static Long getLongTimeOfSSS(String time) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
            Date date = sdf.parse(time);
            return date.getTime();
        } catch (Exception e) {
        }
        return 0L;
    }

    /**
     * 将字符串时间转为Long时间
     *
     * @param time yyyy-MM-dd HH:mm:ss
     */
    public static Long getLongTime(String time) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = sdf.parse(time);
            return date.getTime();
        } catch (Exception e) {
        }
        return 0L;
    }

    /**
     * 将字符串时间转为Long时间
     *
     * @param time yyyy-MM-dd
     */
    public static Long getLongTimeOfYMD(String time) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date date = sdf.parse(time);
            return date.getTime();
        } catch (Exception e) {
        }
        return 0L;
    }

    /**
     * 将Long时间转成String时间
     *
     * @return yyyy-MM-dd HH:mm:ss:SSS
     */
    public static String getStringTimeOfSSS(Long time) {
        Date date = new Date(time);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
        return sdf.format(date);
    }

    /**
     * 将Long时间转成String时间
     *
     * @return yyyy-MM-dd HH:mm:ss
     */
    public static String getStringTime(Long time) {
        Date date = new Date(time);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date);
    }

    /**
     * 将Long时间转成String时间
     *
     * @return yyyy-MM-dd
     */
    public static String getStringTimeOfYMD(Long time) {
        Date date = new Date(time);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(date);
    }

    /**
     * 当前的时间(设备)
     *
     * @return yyyy-MM-dd HH:mm:ss:SSS
     */
    public static String getDeviceTimeOfSSS() {
        String date = "";
        try {
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
            date = df.format(new Date());
        } catch (Exception e) {
            e.printStackTrace();
            date = new Date().getTime() + "";//当前时间的long字符串
        }
        return date;
    }

    /**
     * 当前的时间(设备)
     *
     * @return 时间戳
     */
    public static long getDeviceTimeOfTimestamp() {
        long date;
        date = new Date().getTime();
        return date;
    }

    /**
     * 当前的时间(设备)
     *
     * @return yyyy-MM-dd HH:mm:ss
     */
    public static String getDeviceTime() {
        String date = "";
        try {
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            date = df.format(new Date());
        } catch (Exception e) {
            e.printStackTrace();
            date = new Date().getTime() + "";//当前时间的long字符串
        }
        return date;
    }

    /**
     * 当前的时间(年月日)
     *
     * @return yyyy-MM-dd
     */
    public static String getDeviceTimeOfYMD() {
        String date = "";
        try {
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            date = df.format(new Date());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 当前的时间(年月日)
     *
     * @return HH:mm:ss
     */
    public static String getDeviceTimeOfHms() {
        String date = "";
        try {
            SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
            date = df.format(new Date());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 当前的时间(年月)
     *
     * @return yyyy-MM
     */
    public static String getDeviceTimeOfYM() {
        String date = "";
        try {
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM");
            date = df.format(new Date());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 当前的时间(时分)
     *
     * @return HH:mm
     */
    public static String getDeviceTimeOfHm() {
        String date = "";
        try {
            SimpleDateFormat df = new SimpleDateFormat("HH:mm");
            date = df.format(new Date());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 获取某月最后一天(年月日)
     *
     * @return yyyy-MM
     */
    public static String getLastDayOfMonthOfYMD(int year, int month) {
        Calendar cal = Calendar.getInstance();
        // 设置年份
        cal.set(Calendar.YEAR, year);
        // 设置月份
        cal.set(Calendar.MONTH, month - 1);
        // 获取某月最大天数
        int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        // 设置日历中月份的最大天数
        cal.set(Calendar.DAY_OF_MONTH, lastDay);
        // 格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String lastDayOfMonth = sdf.format(cal.getTime());

        return lastDayOfMonth;
    }

    /**
     * 获取某月最后一天(日)
     */
    public static int getLastDayOfMonth(int year, int month) {
        Calendar cal = Calendar.getInstance();
        // 设置年份
        cal.set(Calendar.YEAR, year);
        // 设置月份
        cal.set(Calendar.MONTH, month - 1);
        // 获取某月最大天数
        int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);

        return lastDay;
    }

    /**
     * 日期转星期
     *
     * @param datetime
     * @return
     */
    public static String dateStrToWeek(String datetime) {
        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
        String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Calendar cal = Calendar.getInstance(); // 获得一个日历
        Date datet = null;
        try {
            datet = f.parse(datetime);
            cal.setTime(datet);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1; // 指示一个星期中的某天。
        if (w < 0)
            w = 0;
        return weekDays[w];
    }

    /**
     * String转yyyy-MM-dd
     *
     * @param str
     * @return
     */
    public static String dateToStringYMD(String str) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date date = sdf.parse(str);
            str = sdf.format(date);
            return str;
        } catch (Exception e) {
        }
        return "";
    }


    /**
     * Date转yyyy-MM-dd
     *
     * @param date
     * @return
     */
    public static String dateToStringYMDD(Date date) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String str = sdf.format(date);
            return str;
        } catch (Exception e) {
        }
        return "";
    }


    /**
     * Date转yyyy-MM-dd
     *
     * @param date
     * @return
     */
    public static String dateToStringYM(Date date) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
            String str = sdf.format(date);
            return str;
        } catch (Exception e) {
        }
        return "";
    }

    /**
     * String转HH:mm:ss
     *
     * @param str
     * @return
     */
    public static String dateToStringHMS(String str) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
            Date date = sdf.parse(str);
            str = sdf.format(date);
            return str;
        } catch (Exception e) {
        }
        return "";
    }

    /**
     * 字符串转date yyyy-MM-dd
     *
     * @param time
     * @return
     */
    public static String stringToYMZ(String time) {
        try {
            SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM"); //加上时间
            SimpleDateFormat sDateFormat2 = new SimpleDateFormat("yyyy年MM月"); //加上时间
            Date date = null;
            //必须捕获异常
            date = sDateFormat.parse(time);
            String str = sDateFormat2.format(date);
            return str;
        } catch (ParseException px) {
            px.printStackTrace();
        }
        return "";
    }

    /**
     * 字符串转date yyyy-MM HH:mm
     *
     * @param time
     * @return
     */
    public static Date stringToYMZHM(String time) {
        Date date = null;
        try {
            SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM HH:mm"); //加上时间
            //必须捕获异常
            date = sDateFormat.parse(time);
            return date;
        } catch (ParseException px) {
            px.printStackTrace();
        }
        return date;
    }

    /**
     * 时间（2019-07-03T10:17:51.000+0000转ymdhm）
     *
     * @param time
     * @return
     */
    public static String stringTToStringYMDHM(String time) {

        DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSSZ");
        DateFormat df2 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        df2.setTimeZone(TimeZone.getTimeZone("GMT"));
        Date date = null;
        try {
            date = df.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return df2.format(date);
    }

    /**
     * 时间（2019-07-03T10:17:51.000+0000转mdhm）
     *
     * @param time
     * @return
     */
    public static String stringTToStringMDHM(String time) {

        DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSSZ");
        DateFormat df2 = new SimpleDateFormat("MM-dd  HH:mm");
        df2.setTimeZone(TimeZone.getTimeZone("GMT"));
        Date date = null;
        try {
            date = df.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return df2.format(date);
    }

    /**
     * 字符串转date yyyy-MM-dd
     *
     * @param time
     * @return
     */
    public static Date stringToDate(String time) {
        SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd"); //加上时间
        Date date = null;
        //必须捕获异常
        try {
            date = sDateFormat.parse(time);
            System.out.println(date);
        } catch (ParseException px) {
            px.printStackTrace();
        }
        return date;
    }

    /**
     * String转yyyy-MM-dd
     *
     * @param str
     * @return
     */
    public static Date stringToDateYMD(String str) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date date = sdf.parse(str);
            return date;
        } catch (Exception e) {
        }
        return null;
    }

    /**
     * String转HH:mm
     *
     * @param str
     * @return
     */
    public static Date stringToDateHm(String str) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
            Date date = sdf.parse(str);
            return date;
        } catch (Exception e) {
        }
        return null;
    }


    /**
     * 获得两个时间的时间差（天）
     *
     * @param startTime 小时间
     * @param endTime   大时间
     * @return
     */
    public static int getDateDifftime(Date startTime, Date endTime) {
        int days = 0;
        long time1 = startTime.getTime();
        long time2 = endTime.getTime();

        long diff;
        if (time1 < time2) {
            diff = time2 - time1;
        } else {
            diff = time1 - time2;
        }
        days = (int) (diff / (24 * 60 * 60 * 1000));
        return days;
    }

    /**
     * 获得两个时间的时间差（小时）
     *
     * @param startTime 小时间
     * @param endTime   大时间
     * @return
     */
    public static double getDateDifftime2(Date startTime, Date endTime) {
        double hour = 0;
        long time1 = startTime.getTime();
        long time2 = endTime.getTime();

        long diff;
        diff = time2 - time1;
        hour = (diff / (60 * 60 * 1000));
        return hour;
    }

    public static boolean lastDateIsThisDateDay(long lastDate, long thisDate) {
        Date date = new Date(lastDate);
        Date date2 = new Date(thisDate);
        Calendar calendar = Calendar.getInstance();
        Calendar calendar2 = Calendar.getInstance();
        calendar.setTime(date);//给calendar赋值
        int y1 = calendar.get(Calendar.YEAR);//获取年份
        int d1 = calendar.get(Calendar.DAY_OF_YEAR);//获取年中第几天
        calendar2.setTime(date2);
        int y2 = calendar2.get(Calendar.YEAR);
        int d2 = calendar2.get(Calendar.DAY_OF_YEAR);
        if (y1 == y2 && d1 == d2)//判断是不是同一年的同一天
            return true;
        else
            return false;
    }

    //时间往后推n天
    public static Date dateAddNDay(Date date, Integer day) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date); //需要将date数据转移到Calender对象中操作
        calendar.add(calendar.DATE, day);//把日期往后增加n天.正数往后推,负数往前移动
        date = calendar.getTime();   //这个时间就是日期往后推一天的结果
        return date;
    }

}